<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SalesForceEvent
 *
 * @author Mubashar
 */
class SalesForceEvent {

    private $insightUser;

    public function __construct($insightUser) {
        $this->insightUser = $insightUser;
    }

    public function getEvent($id) {
        
    }

    public function getEvents() {
        $insightUser = $this->insightUser;
        $userId = $insightUser->user_id;
        $query = "Select " . implode(',', $insightUser->getAccessibleFields("Event")) . ", (Select Id, EventId, AttendeeId, Attendee.Name, Status, RespondedDate, Response, LastModifiedDate, IsDeleted From EventAttendees) From Event Where OwnerId='{$userId}'";
        $result = SalesforceClient::queryRecords($query, "query");
        
        $arrEvents = array();
        
        $mapper = new SalesForceEventView($insightUser);
        foreach ($result["queryResult"]->records as $record) {
//            $objSalesForceEventPoco = new SalesForceEventPoco();
//            
//            $objSalesForceEventPoco->Id = $record->Id;
//            $objSalesForceEventPoco->ActivityDate = $record->fields->ActivityDate;
//            $objSalesForceEventPoco->ActivityDateTime = $record->fields->ActivityDateTime;
//            //$objSalesForceEventPoco->Attendees = $record->Attendees;
//            $objSalesForceEventPoco->Description = $record->fields->Description;
//            $objSalesForceEventPoco->EndDateTime = $record->fields->EndDateTime;
//            $objSalesForceEventPoco->IsAllDayEvent = $record->fields->IsAllDayEvent;
//            $objSalesForceEventPoco->IsDeleted = $record->fields->IsDeleted;
//            $objSalesForceEventPoco->IsPrivate = $record->fields->IsPrivate;
//            $objSalesForceEventPoco->IsRecurrence = $record->fields->IsRecurrence;
//            $objSalesForceEventPoco->IsReminderSet = $record->fields->IsReminderSet;
//            $objSalesForceEventPoco->Location = $record->fields->Location;
//            $objSalesForceEventPoco->LastModifiedDate = $record->fields->LastModifiedDate;
//            $objSalesForceEventPoco->OwnerId = $record->fields->OwnerId;
//            $objSalesForceEventPoco->ReminderDateTime = $record->fields->ReminderDateTime;
//            $objSalesForceEventPoco->ShowAs = $record->fields->ShowAs;
//            $objSalesForceEventPoco->StartDateTime = $record->fields->StartDateTime;
//            $objSalesForceEventPoco->Subject = $record->fields->Subject;
//            $objSalesForceEventPoco->Type = $record->fields->Type;
//            $objSalesForceEventPoco->WhoId = $record->fields->WhoId;
            $objSalesForceEventPoco = $mapper->createModel($record);
            $arrEvents[$objSalesForceEventPoco->Id] = $objSalesForceEventPoco;
        }
        return $arrEvents;
    }

    /* public function getEvents() {
      $userId = $insightUser->user_id;
      $query = "Select ".impload(',', $insightUser->getAccessibleFields("Event")).", (Select Id, EventId, AttendeeId, Status, RespondedDate, Response, LastModifiedDate, IsDeleted From EventAttendees) From Event Where OwnerId='{$userId}'";
      $result = SalesforceClient::queryRecords($query);

      $mapper = new SalesforceEventMapper($this->insightUser);
      $arrEvents = array();
      foreach($result->records as $record) {
      $arrEvent[] = $mapper->createModel($record);
      }
      return $arrEvents;
      } */

    public function insertEvents(array $data) {
        $mapper = new SalesForceEventView($this->insightUser);
        $records = array();
        $results = array();

        $count = 0;
        foreach ($data as $event) {
            $records[] = $mapper->createStdObject($event);
            $count++;

            if ($count == 200 || $count == count($data)) {
                $result = SalesforceClient::insert($records);
                if(!is_array($result)) {
                    $result = array($result);
                }
                $results = array_merge($results, $result);

                $records = array();
                $count = 0;
            }
        }
        return results;
    }

    public function updateEvents(array $data) {
        $mapper = new SalesForceEventView($this->insightUser);
        $records = array();
        $results = array();

        $count = 0;
        foreach ($data as $event) {
            $records[] = $mapper->createStdObject($event);
            $count++;

            if ($count == 200 || $count == count($data)) {
                $result = SalesforceClient::update($records);
                if(!is_array($result)) {
                    $result = array($result);
                }
                $results = array_merge($results, $result);
                
                $records = array();
                $count = 0;
            }
        }
        return results;
    }

    public function upsertEvents(array $data) {
        $mapper = new SalesForceEventView($this->insightUser);
        $records = array();
        $results = array();

        $count = 0;
        foreach ($data as $event) {
            $records[] = $mapper->createStdObject($event);
            
            $count++;
            
            if ($count == 200 || $count == count($data)) {
                CalendarSyncUtil::debug("records for upsert to Salesforce", $records);
                $result = SalesforceClient::upsert($records);
                if(!is_array($result)) {
                    $result = array($result);
                }
                $results = array_merge($results, $result);

                $records = array();
                $count = 0;
            }
        }
        return $results;
    }

    public function deleteEvents(array $data) {
        $Ids = array();
        $results = array();

        $count = 0;
        foreach ($data as $event) {
            $Ids[] = $event->Id;
            $count++;

            if ($count == 200 || $count == count($data)) {
                $result = SalesforceClient::delete($Ids);
                if(!is_array($result)) {
                    $result = array($result);
                }
                $results = array_merge($results, $result);

                $Ids = array();
                $count = 0;
            }
        }
        return $results;
    }

}

?>
